﻿using FarseerPhysics;
using FarseerPhysics.Collision.Shapes;
using FarseerPhysics.Common;
using FarseerPhysics.Dynamics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GameName1
{
    class BrickWall : GameComponent
    {
        private Texture2D _texture;
        private Vector2 _origin;
        private Vector2 _pixelSize;
        private Vector2 _size;
        private Body _left;
        private Body _top;
        private Body _right;
        private Body _bottom;
        private float _pixelWidth;
        private float _pixelHeight;
        private float _width;
        private float _height;
        private float _pixelThickness;
        private float _thickness;
        public BrickWall(Game game, World world, string textureName, Vector2 initialPositionSim)
            : base(game, initialPositionSim,world)
        {
            _texture = Game.Content.Load<Texture2D>(textureName);
            _pixelThickness  = _texture.Width;
            _thickness = ConvertUnits.ToSimUnits(_pixelThickness);
            _pixelWidth = Game.GraphicsDevice.Viewport.Width;
            _pixelHeight = Game.GraphicsDevice.Viewport.Height;
            _width = ConvertUnits.ToSimUnits(_pixelWidth);
            _height = ConvertUnits.ToSimUnits(_pixelHeight);

            _origin = _pixelSize / 2f;
            _size = ConvertUnits.ToSimUnits(_pixelSize);

            PolygonShape box;
            _left = new Body(world);
            _left.BodyType = BodyType.Static;
            _left.Position = new Vector2(0f, 0f);
            Vector2[] vlist_left = 
            {
                new Vector2(0f,0f)
                ,new Vector2(0f+_thickness,0)
                ,new Vector2(0f+_thickness,_height)
                ,new Vector2(0f,_height)
            };
            Vertices vertices_left = new Vertices(vlist_left);
            box = new PolygonShape(vertices_left, 1);
            _left.CreateFixture(box);

            _top = new Body(world);
            _top.BodyType = BodyType.Static;
            _top.Position = new Vector2(0f+_thickness, 0f);
            Vector2[] vlist_top = 
            {
                new Vector2(0f,0f)
                ,new Vector2(0f+_width-2*_thickness,0)
                ,new Vector2(0f+_width-2*_thickness,_thickness)
                ,new Vector2(0f,_thickness)
            };
            Vertices vertices_top = new Vertices(vlist_top);
            box = new PolygonShape(vertices_top, 1);
            _top.CreateFixture(box);

            _right = new Body(world);
            _right.BodyType = BodyType.Static;
            _right.Position = new Vector2(0f + _width-_thickness, 0f);
            Vector2[] vlist_right = 
            {
                new Vector2(0f,0f)
                ,new Vector2(0f+_thickness,0)
                ,new Vector2(0f+_thickness,_height)
                ,new Vector2(0f,_height)
            };
            Vertices vertices_right = new Vertices(vlist_right);
            box = new PolygonShape(vertices_right, 1);
            _right.CreateFixture(box);

            _bottom = new Body(world);
            _bottom.BodyType = BodyType.Static;
            _bottom.Position = new Vector2(0f + _thickness, _height-_thickness);
            Vector2[] vlist_bottom = 
            {
                new Vector2(0f,0f)
                ,new Vector2(0f+_width-2*_thickness,0)
                ,new Vector2(0f+_width-2*_thickness,_thickness)
                ,new Vector2(0f,_thickness)
            };
            Vertices vertices_bottom = new Vertices(vlist_bottom);
            box = new PolygonShape(vertices_bottom, 1);
            _bottom.CreateFixture(box);


        }

        public override void Draw(GameTime gameTime, SpriteBatch batch)
        {
            //left
            for (int i = 0; i < _pixelHeight / _pixelThickness; i++)
            {
                Vector2 position = new Vector2(0f, i*_pixelThickness);
                batch.Draw(_texture, position, Color.White);
            }
            //top
            for (int i = 0; i < (_pixelWidth-2*_pixelThickness) / _pixelThickness; i++)
            {
                Vector2 position = new Vector2(_pixelThickness+i * _pixelThickness, 0);
                batch.Draw(_texture, position, Color.White);
            }
            //right
            for (int i = 0; i < _pixelHeight / _pixelThickness; i++)
            {
                Vector2 position = new Vector2(_pixelWidth-_pixelThickness, i * _pixelThickness); ;
                batch.Draw(_texture, position, Color.White);
            }
            //bottom
            for (int i = 0; i < (_pixelWidth - 2 * _pixelThickness) / _pixelThickness; i++)
            {
                Vector2 position = new Vector2(_pixelThickness + i * _pixelThickness, _pixelHeight - _pixelThickness);
                batch.Draw(_texture, position, Color.White);
            }
            //batch.Draw(_texture, new Vector2(0f,0f), Color.White);
        }
    }


}
